﻿using System;
using System.Security;
using System.Web;
using MAF.Common;
using MAF.UserSecurity;

namespace My.Blog.Web
{
    public class SecurityUI
    {
        /// <summary>
        ///  常量
        /// </summary>
        public const string ROOT_URL = "RootUrl";
        public const string PAGE_SIZE = "PageSize";
        public const string VALIDATE_CODE = "MyValidateCode";
        public const string MAINTAIN_MODE = "MaintainMode";
        public const string YES = "yes";

        /// <summary>
        /// 是否已经登录
        /// </summary>
        /// <returns></returns>
        public static bool IsLogon()
        {
            return UserContext.Current.IsLogon;
        }

        /// <summary>
        /// 登录(验证)
        /// </summary>
        public static string LogonUser()
        {
            HttpContext c = HttpContext.Current;
            ////////////////////////////////////////////////////////////
            // 输入用户名，密码
            string name = c.Request.Form["Name"];
            string psw = c.Request.Form["Password"];
            string isKeepLogon = c.Request.Form["KeepLogon"];
            string gotoHere = c.Request.Form["Goto"];

            try
            {
                DateTime expired = DateTime.Now.AddHours(1);
                if("Yes" == isKeepLogon) 
                {
                    expired = expired.AddMonths(3);
                }
                // 调用登陆服务
                Authenticator.Login(name, psw, expired);

                // 返回成功自动跳转到进来页或首页
                if (string.IsNullOrEmpty(gotoHere))
                    c.Response.Redirect(c.Request.ApplicationPath);
                else
                    c.Response.Redirect(gotoHere);

                return "登录成功";
            }
            // 否则，抛出异常
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 退出
        /// </summary>
        public static string LogoutUser()
        {
            HttpContext c = HttpContext.Current;
            try
            {
                Authenticator.Logout();

                // 自动跳转到进来页
                c.Response.Redirect(c.Request.UrlReferrer.ToString());

                return "退出成功";
            }
            catch(Exception ex)
            {
                return "退出失败" + ex.Message;
            }
        }

        /// <summary>
        /// 是否维护模式
        /// </summary>
        /// <param name="role">role有个特殊值-1,表示不用查看权限</param>
        /// <returns></returns>
        public static bool IsMaintainMode(string role)
        {
            if (WebHelper.GetCookie(MAINTAIN_MODE) == YES)
            {
                if ("-1"==role)
                    return true;
                else
                    return UserContext.Current.IsInRole(role);
            }
            return false;
        }

        /// <summary>
        ///  进入维护模式
        /// </summary>
        public static void EntryMaintainMode()
        {
            HttpContext context = HttpContext.Current;
            context.Response.Cookies.Add(new HttpCookie(MAINTAIN_MODE, YES));
            context.Response.Redirect(context.Request.UrlReferrer.ToString());
        }

        /// <summary>
        /// 退出维护模式
        /// </summary>
        public static void ExitMaintainMode()
        {
            HttpContext context = HttpContext.Current;
            WebHelper.RemoveCookie(MAINTAIN_MODE);
            context.Response.Redirect(context.Request.UrlReferrer.ToString());
        }
    }
}